import {PageContainer, ProFormRadio} from '@ant-design/pro-components';
import {request, useRequest} from '@umijs/max';
import {Button, Card, Form, Input} from 'antd';

type ChatProps = {
    callback?: (data: any) => void;
}

const Chat: React.FC<ChatProps> = ({callback}) => {
    const {run, data, loading} = useRequest(
        (params: {message: string; apiKey: string}) => {
            return request('/api/chat', {
                method: 'post',
                data: params,
                headers: {
                    'Api-Key': params.apiKey,
                },
            });
        },
        {
            manual: true,
            onSuccess: (data) => {
                try {
                    callback?.(data)
                } catch (error) {
                    console.error(error);
                }
            }
        },
    );

    return (
        <PageContainer ghost>
            <div>
                <Card title="返回内容">{JSON.stringify(data)}</Card>

                <Form
                    onFinish={(values) => {
                        if (!values.message) return;

                        run({...values});
                        // callback?.({
                        //     "actions": [
                        //         {
                        //             "actionType": "create card event",
                        //             "eventReference": {
                        //                 "size": "small",
                        //                 "title": "小测试卡片",
                        //                 "children": "为什么"
                        //             },
                        //             "type": "Card"
                        //         }
                        //     ]
                        // })
                    }}
                    initialValues={{
                        apiKey: 'sk-bqqqqpkyojneqipkncoxnhdxypouyyfpiwvwcsdbvhitewmq',
                    }}
                >
                    <Form.Item label="ApiKey" name={'apiKey'}>
                        <Input placeholder="请输入ApiKey" />
                    </Form.Item>
                    <Form.Item label="对话" name={'message'}>
                        <Input.TextArea placeholder="请输入聊天内容" style={{height: 150}} />
                    </Form.Item>
                    <Form.Item>
                        <Button loading={loading} htmlType='submit' type="primary">
                            提交
                        </Button>
                    </Form.Item>
                </Form>
            </div>
        </PageContainer>
    );
};

export default Chat;
